package com.guoguo.service.impl;

import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.entity.Menus;
import com.guoguo.entity.vo.MenusVO;
import com.guoguo.mapper.MenusMapper;
import com.guoguo.service.MenuService;
import com.guoguo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by KLSD on 2017/9/13.
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenusMapper menusMapper;


    @Override
    public PageBean<Menus> selectMenuList(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageBean<Menus>(menusMapper.selectMenuList());
    }

    @Override
    public Menus selectByPrimaryKey(String menuId) {
        return menusMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public int deleteByPrimaryKey(String menuId) {
        return menusMapper.deleteByPrimaryKey(menuId);
    }

    @Override
    public Menus addMenu(String menuId, String menuName, String menuDescription, String menuUrl, String menuParent, int menuType) {
        Menus menus = menusMapper.selectByPrimaryKey(menuId);
        if (menus == null) {
            menus = new Menus(menuId, menuName, menuDescription, menuUrl, menuParent, menuType);
            if (StringUtils.isEmptyString(menuParent))
                menus.setMenuParent("0");
            int num = menusMapper.insertSelective(menus);
            return menus;

        }
        menus.setMenuId(menuId);
        menus.setMenuName(menuName);
        menus.setMenuDescription(menuDescription);
        menus.setMenuUrl(menuUrl);
        menus.setMenuParent(menuParent);
        menus.setMenuType(menuType);
        int num = menusMapper.updateByPrimaryKeySelective(menus);
        return menus;
    }

    @Override
    public List<Menus> selectMenus() {
        return menusMapper.selectMenus();
    }

    public List<MenusVO> getMenus() {
        List<Menus> menuses = menusMapper.getMenus("-1");
        List<MenusVO> menusVOs = new ArrayList<>();
        for (Menus menuse : menuses) {

        }
        return null;
    }

    private List<MenusVO> getMenus(String menuParent) {
        List<Menus> menuses = menusMapper.getMenus(menuParent);
        if (menuses.size() > 0) {
            for (Menus menuse : menuses) {

            }
        }
        return null;
    }
}
